package com.zijie.shopcategoryapi.category.api;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.zijie.category.model.Category;
import com.zijie.category.service.CategoryService;
import com.zijie.core.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;


@RestController
@RequestMapping("/api/v1/category")
public class CategoryApi {
    private CategoryService categoryService;

    @Autowired
    public void setBrandService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    @SentinelResource(value = "category",fallback = "fallFindAll" ,blockHandler = "findAllBlock")
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(Category category) {
        List<Category> categoryList = categoryService.findAll(category);
//        Random random = new Random();
//        if (random.nextInt(10)>5){
//            throw new RuntimeException("服务器异常");
//        }
        return ResponseEntity.ok(JsonResult.success(categoryList));
    }
//    降级  这里是sentinel出现异常会进入到的地方
    public ResponseEntity<JsonResult<?>> blockAll(BlockException blockException) {
        if (blockException instanceof FlowException){
            return ResponseEntity.ok(JsonResult.error("429","限流流控1"));
        }
        return ResponseEntity.ok(JsonResult.error("429","限流熔断,哈哈哈"));
    }
//    异常处理  业务异常
    public ResponseEntity<JsonResult<?>> fallFindAll(Category category, Throwable e) {
        if (e instanceof DegradeException){
            return ResponseEntity.ok(JsonResult.error("429","熔断了"));
        }
        return ResponseEntity.ok(JsonResult.error("999",e.getMessage()));
    }
    @GetMapping("/all")
    public ResponseEntity<JsonResult<?>> findAll() {
        List<Category> categoryList = categoryService.findAll();
        return ResponseEntity.ok(JsonResult.success(categoryList));
    }
    @GetMapping("/tree")
    public ResponseEntity<JsonResult<?>> findTree() {
        List<Category> categoryList = categoryService.findTree();
        return ResponseEntity.ok(JsonResult.success(categoryList));
    }
//    添加
    @PostMapping("/add")
    public ResponseEntity<JsonResult<?>> add(@RequestBody Category category) {
        return ResponseEntity.ok(JsonResult.success(categoryService.add( category)));
    }
//    修改
    @PutMapping("/update")
    public ResponseEntity<JsonResult<?>> update(@RequestBody Category category) {
        return ResponseEntity.ok(JsonResult.success(categoryService.update(category)));
    }
//    删除多个
    @DeleteMapping("/delete")
    public ResponseEntity<JsonResult<?>> delete(@RequestParam("ids") List<Integer> ids) {
        int count=0;
        for (Integer id : ids) {
            count += categoryService.deleteById(id);
        }
        if (count==0){
            return ResponseEntity.ok(JsonResult.error("未找到该分类"));
        }else {
            return ResponseEntity.ok(JsonResult.success(count));
        }
    }
    @GetMapping("/findById/{id}")
    public ResponseEntity<JsonResult<?>> findById(@PathVariable Integer id) {
        Category category = categoryService.findById(id);
        if (category == null) {
            return ResponseEntity.ok(JsonResult.error("未找到该分类"));
        }
        return ResponseEntity.ok(JsonResult.success(category));
    }
}
